/**
 * Copyright (c) 2008 - TheNittyGritty - Thomas Brekelmans
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
package nl.thenittygritty.ui 
{
	import flash.utils.Dictionary;		

	/**
	 * MultipleKeysManager is a Singleton class which provides an isKeyDown 
	 * method which keeps track of multiple keys at once. 
	 * (See addKey and removeKey).
	 */
	public class MultipleKeysManager 
	{
		private static var instance:MultipleKeysManager;
		
		private var keyList:Dictionary;
		
		/**
		 * Creates a new MultipleKeysManager, this class shouldn't 
		 * be instatiated.
		 * 
		 * <p>Use MultipleKeysManager.getInstance() to get the currently active 
		 * instance of MultipleKeysManager.</p>
		 */
		public function MultipleKeysManager(singletonEnforcer:SingletonEnforcer) 
		{
			keyList = new Dictionary();
		}
		
		/**
		 * Returns the currently active MultipleKeysManager instance.
		 * 
		 * <p>If no instance is defined, it's created here.</p>
		 */
		public static function getInstance():MultipleKeysManager
		{
			if (instance == null)
			{
				instance = new MultipleKeysManager(new SingletonEnforcer());	
			}	
			return instance;
		}
		
		/**
		 * Adds the given key to it's internal store of currently pressed keys.
		 * 
		 * <p>Invoke this method in your KeyboardEvent.KEY_DOWN handler and 
		 * feed it the keyboardEvent.keyCode parameter.</p>
		 */
		public function addKey(key:uint):void
		{
			keyList.key = key;
		}

		/**
		 * Removes the given key of it's internal store of currently 
		 * pressed keys.
		 * 
		 * <p>Invoke this method in your KeyboardEvent.KEY_UP handler and 
		 * feed it the keyboardEvent.keyCode parameter.</p>
		 */
		public function removeKey(key:uint):void
		{
			keyList.key = null;
		}
		
		/**
		 * Returns whether or not the given key is down (i.e. in this class's 
		 * internal store).
		 */
		public function isKeyDown(key:uint):Boolean
		{
			return (keyList.key === key);	
		}

		/**
		 * Returns the name of the class in a String.
		 */
		public function toString():String 
		{
			return "MultipleKeysManager";
		}
	}
}

/**
 * Internal helper class which prevents instantiation from the 
 * MultipleKeysManager from outside this file.
 */
internal class SingletonEnforcer {}
